package com.duyutian.controller.manageController;

import com.duyutian.common.CusAccessObjectUtil;
import com.duyutian.common.ServiceResult;
import com.duyutian.domain.LayUiData;
import com.duyutian.model.ArticleComment;
import com.duyutian.model.User;
import com.duyutian.util.NumberUtils;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by dyt on 2018/10/9.
 */
@Controller
@RequestMapping("/cms")
public class CommentCmsController {
    @RequestMapping("/commentList")
    @ResponseBody
    public LayUiData getComment(int page ,int limit,Integer status) {
        //通过Statu 1 表示未回复 2 表示已经回复  all 表示查看所有的
        int start = (page-1)*limit;
        StringBuilder sql = new StringBuilder("SELECT\n" +
                "\ta.*,\n" +
                "\tb.title title \n" +
                "FROM\n" +
                "\tarticle_comment a,\n" +
                "\tarticle b \n" +
                "WHERE\n" +
                "\ta.article_id = b.id and a.is_delete = 0 and parent_id = 0 ");
        //查询回复状态
        if(status!=null&&status!=0) {
            sql.append(" and a.status = " + status);
        }
        //计算评论数量
        List<Record> count = Db.find(sql.toString());
        sql.append(" order by created desc");
        sql.append(" limit "+start+", "+limit);
        List<Record> records = Db.find(sql.toString());
        List<Map> data = ServiceResult.rsFormat1(records);
        return LayUiData.success(count.size(),data);
    }
    /*留言回复接口*/
    @RequestMapping("/commentReply")
    @ResponseBody
    public ServiceResult commentReply(Integer id , String content, HttpServletRequest request) {
        ArticleComment comment = ArticleComment.dao.findById(id);
        //从session中获取用户
        User user = (User) request.getSession().getAttribute("user");
        ArticleComment articleComment = new ArticleComment();
        articleComment.setUserId(user.getId());
        articleComment.setArticleId(comment.getArticleId());
        articleComment.setIp(CusAccessObjectUtil.getIpAddress(request));
        articleComment.setAuthor(user.getNickname());
        articleComment.setParentId(NumberUtils.parseLong(id,10000));
        articleComment.setCreated(new Date());
        articleComment.setContent(content);
        articleComment.save();
        comment.setStatus("2");
        comment.setReplyDate(new Date());
        comment.setReply(content);
        comment.update();
        return ServiceResult.success();
    }
    /*删除留言接口*/
    @RequestMapping("/deleteReply")
    @ResponseBody
    public ServiceResult deleteReply(Integer id) {
        ArticleComment comment = ArticleComment.dao.findById(id);
        comment.setIsDelete("1");
        comment.update();
        return ServiceResult.success();
    }
}
